/* IconSaver.m created by epeyton on Fri 17-Sep-1999 */

#import "IconSaver.h"

@implementation IconSaver

- (void)drawRect:(NSRect)rects
{
    [[NSColor blackColor] set];
    NSRectFill(rects);
}

- (id)initWithFrame:(NSRect)frameRect
{    
    if (![super initWithFrame:frameRect]) return nil;
    
    iconImageArray = [[NSMutableArray alloc] init];
    
    [NSBundle loadNibNamed:@"Icon" owner:self];


    [self allocateGState];
    srandom(time(0));

    directoryArray = [[self getAvailableIconDirectories:NO] retain];
    
    return self;
}

- (void)setFrameSize:(NSSize)newSize
{
    if ((newSize.width > 640) && (newSize.width > 480)) {
	canDraw = YES;
    } else {
	canDraw = NO;
    }
    width = newSize.width;
    height = newSize.height;
    [super setFrameSize:newSize];
    return;
}

    

- (NSTimeInterval)animationTimeInterval
{
    return 0.1;
}

- (void)recreateIconPaths:sender
{
    [self getAvailableIconDirectories:YES];
}


- (NSArray *)getAvailableIconDirectories:(BOOL)recreate
{
    NSArray *retArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"IconPathArray"];

    if (!retArray  || recreate) {
    	NSMutableArray *mutArray = [NSMutableArray array];
    
		// search /Local/Applications
    	[mutArray addObjectsFromArray:[self arrayAtLocation:@"/Applications"]];
		// search /System/Developer/Applications
    	[mutArray addObjectsFromArray:[self arrayAtLocation:@"/Developer/Applications"]];
		// search ~/Applications
    	[mutArray addObjectsFromArray:[self arrayAtLocation:[NSString stringWithFormat:@"%@/%@", NSHomeDirectory(),@"Applications"]]];

        retArray = [NSArray arrayWithArray:mutArray];

        [[NSUserDefaults standardUserDefaults] setObject:retArray forKey:@"IconPathArray"];
    }
    
    return retArray;
}

- (NSArray *)arrayAtLocation:(NSString *)location
{
    id file;
    
    NSMutableArray *mutArray = [NSMutableArray array];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
          enumeratorAtPath:location];
    while (file = [enumerator nextObject]) {
        if ([[file pathExtension] isEqualToString:@"app"])
             [mutArray addObject:[NSString stringWithFormat:@"%@/%@",location, file]];
    }
    return [NSArray arrayWithArray:mutArray];
}

- (NSRect)randomRect
{
    BOOL found = NO;
    NSRect currentRect;

    while (!found) {
        // pick a random point and size
        NSPoint point;
        NSSize size;
        int factor = SSRandomFloatBetween(1.0,5.0);
        NSEnumerator *iconImageEnum;
        id anIconImage;

pickNew:
	iconImageEnum = [iconImageArray objectEnumerator];

        point = NSMakePoint(SSRandomFloatBetween(0.0, width),SSRandomFloatBetween(0.0, height));

        size = NSMakeSize(48*factor, 48*factor);

        currentRect = NSMakeRect(point.x, point.y, size.width, size.height);

        
        // see if these fall into any of the rects already doled out
        while ((anIconImage = [iconImageEnum nextObject])) {
            if (NSIntersectsRect([anIconImage imageRect] , currentRect)) {
                goto pickNew;
		
            }
        }
        found = YES;
    }

    // return the final rect (made from rect and size
    return currentRect;
}

- (void)oneStep
{
    NSEnumerator *iconImageEnum = [iconImageArray objectEnumerator];
    id anIconImage;
    

    
    if (!canDraw) {
	return;
    }

    while (anIconImage = [iconImageEnum nextObject]) {
        [anIconImage display];
    }
    
    return;
}

- (void)removeIconImage:(IconImage *)iconImage
{

    // create a new image too
    [iconImageArray addObject:[[[IconImage alloc] initWithPath:[directoryArray objectAtIndex:SSRandomFloatBetween(0, [directoryArray count] - 1)] inRect:[self randomRect] parent:self] autorelease]];

    [iconImageArray removeObject:iconImage];
}

- (void) startAnimation
{
    int initialImages = SSRandomFloatBetween(8.0, 15.0);
    int i = 0;
    if (!canDraw) {
	return;
    }
    

    for (i = 0;i < initialImages - 1;i++) {
        [iconImageArray addObject:[[[IconImage alloc] initWithPath:[directoryArray objectAtIndex:SSRandomFloatBetween(0, [directoryArray count] - 1)] inRect:[self randomRect] parent:self] autorelease]];
    }
    [super startAnimation];
}


- (void) stopAnimation
{

    [iconImageArray removeAllObjects];
    [super stopAnimation];
}

- (BOOL)hasConfigureSheet { return YES; }
- (NSWindow*)configureSheet { return window; }

- (void)closeSheet:(id)ssender
{
    [NSApp endSheet:window];
}


@end
